package com.wyix.controller;

import com.wyix.common.PageResult;
import com.wyix.common.Result;
import com.wyix.domain.dto.ScoreDTO;
import com.wyix.domain.dto.ScoreQueryDTO;
import com.wyix.domain.entity.ScoreDO;
import com.wyix.service.ScoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 成绩控制器
 */
@RestController
@RequestMapping("/api/scores")
@Tag(name = "成绩管理", description = "成绩信息的增删改查接口")
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    /**
     * 新增成绩
     */
    @PostMapping
    @Operation(summary = "新增成绩", description = "添加学生的课程成绩")
    public Result<?> addScore(@Valid @RequestBody ScoreDTO scoreDTO) {
        scoreService.addScore(scoreDTO);
        return Result.success();
    }

    /**
     * 根据ID删除成绩
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除成绩", description = "根据ID删除成绩信息")
    public Result<?> deleteScore(
            @Parameter(description = "成绩ID", required = true) @PathVariable Long id) {
        scoreService.deleteScore(id);
        return Result.success();
    }

    /**
     * 更新成绩信息
     */
    @PutMapping
    @Operation(summary = "更新成绩", description = "更新成绩信息")
    public Result<?> updateScore(@Valid @RequestBody ScoreDTO scoreDTO) {
        scoreService.updateScore(scoreDTO);
        return Result.success();
    }

    /**
     * 根据ID查询成绩
     */
    @GetMapping("/{id}")
    @Operation(summary = "查询成绩", description = "根据ID查询成绩详情")
    public Result<ScoreDO> getScoreById(
            @Parameter(description = "成绩ID", required = true) @PathVariable Long id) {
        ScoreDO score = scoreService.getScoreById(id);
        return Result.success(score);
    }

    /**
     * 多条件分页查询成绩
     */
    @GetMapping
    @Operation(summary = "查询成绩列表", description = "多条件分页查询成绩列表")
    public Result<PageResult<ScoreDO>> queryScores(ScoreQueryDTO queryDTO) {
        PageResult<ScoreDO> pageResult = scoreService.queryScores(queryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据学生ID查询成绩
     */
    @GetMapping("/student/{studentId}")
    @Operation(summary = "查询学生成绩", description = "根据学生ID查询该学生的所有成绩")
    public Result<List<ScoreDO>> getScoresByStudentId(
            @Parameter(description = "学生ID", required = true) @PathVariable Long studentId) {
        List<ScoreDO> scores = scoreService.getScoresByStudentId(studentId);
        return Result.success(scores);
    }

    /**
     * 根据课程ID查询成绩
     */
    @GetMapping("/course/{courseId}")
    @Operation(summary = "查询课程成绩", description = "根据课程ID查询该课程的所有学生成绩")
    public Result<List<ScoreDO>> getScoresByCourseId(
            @Parameter(description = "课程ID", required = true) @PathVariable Long courseId) {
        List<ScoreDO> scores = scoreService.getScoresByCourseId(courseId);
        return Result.success(scores);
    }
}
